﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.Principal;
using System.Web;
using System.Web.Http;
using System.Web.Http.Cors;
using System.Web.Http.Results;
using XQ.WeChatOfficialAccount.BLL.home;
using XQ.WeChatOfficialAccount.DAL;
using XQ.WeChatOfficialAccount.DAL.Model;
using XQ.WeChatOfficialAccount.DAL.Models;
using XQ.WeChatOfficialAccount.WebAPI.Common;
using XQ.WeChatOfficialAccount.WebAPI.Filter;
using XQ.WeChatOfficialAccount.WebAPI.Models;

namespace XQ.WeChatOfficialAccount.WebAPI.Controllers
{
    /// <summary>
    /// 主页信息
    /// </summary>
    /// 
    [FormAuthenticationFilter]
    public class IndexController : ApiController
    {
        /// <summary>
        /// 根据用户获取菜单
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [Route("Api/Index/GetMenu")]
        public ResultModel GetMenu(int userId)
        {
            ResultModel resultModel = new ResultModel();
            try
            {
                TokenModel token = new TokenModel();
                //获取用户信息
                var authorization = Request.Headers.Authorization;
                if (authorization != null)
                {
                    if (authorization.Scheme == "Bearer")
                    {
                        string tokenStr = authorization.Parameter;
                        token = TokenHelper.DecryptToken(tokenStr);
                    }
                }
                IndexBll indexBll = new IndexBll();

                MenuModel menus = indexBll.GetMenusByUser(token.UID, token.DepartmentID);
                
                resultModel.success = true;
                resultModel.code = "10000";
                resultModel.message = "执行成功";
                resultModel.data = menus;
                
            }
            catch (Exception ex)
            {
                resultModel.success = false;
                resultModel.code = "10001";
                resultModel.message = "执行失败";
                resultModel.data = ex.Message;
            }

            return resultModel;
        }

    }
}